import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import("components/login/Login")
const Home = () => import("components/home/Home")
const Welcome = () => import("components/home/children/Welcome")
const Users = () => import("components/home/children/user/User")
const Rights = () => import("components/home/children/rights/Right")
const Roles = () => import("components/home/children/roles/Roles")
const Category = () => import("components/home/children/goods/Category")
const Params = () => import("components/home/children/goods/Params")
const List = () => import("components/home/children/goods/List")
const AddGood = () => import("components/home/children/goods/AddGood")
const Order = () => import("components/home/children/order/Order")
const Report = () => import("components/home/children/report/Report")
Vue.use(VueRouter)

const routes = [{
    path: '',
    redirect: '/login'
  }, {
    path: '/login',
    component: Login
  }, {
    path: '/home',
    component: Home,
    children: [{
        path: '',
        redirect: 'welcome'
      }, {
        path: 'welcome',
        component: Welcome
      }, {
        path: 'users',
        component: Users
      },
      {
        path: 'rights',
        component: Rights
      },
      {
        path: 'roles',
        component: Roles
      },
      {
        path: 'categories',
        component: Category
      },
      {
        path: 'params',
        component: Params
      },
      {
        path: 'addgood',
        name: 'addgood',
        component: AddGood
      },
      {
        path: 'goods',
        component: List,

      },
      {
        path: 'orders',
        component: Order,

      },
      {
        path: 'reports',
        component: Report,

      },




    ]
  }

]

const router = new VueRouter({
  routes,
  mode: "history"
})
router.beforeEach((to, from, next) => {
  //next('/path')强制跳转
  if (to.path == '/login') return next() //如果是访问login直接放行

  const token = sessionStorage.getItem('token')
  if (!token) {
    return next('/login')
  }
  next()

})
export default router